package 寒假刷题;

import org.junit.Test;

import java.util.HashMap;

public class 最常见的单词 {
    public String mostCommonWord(String paragraph, String[] banned) {
        String s = paragraph.toLowerCase();
        s=s.replace('!',' ').replace('?',' ').replace('\'',' ').replace(',',' ').replace(';',' ').replace('.',' ').trim();
        String[] words = s.split(" ");
        HashMap<String, Integer> map = new HashMap<>();
        for (String word : words) {
            if (word.equals(""))
                continue;
            map.put(word,map.getOrDefault(word,0)+1);
        }
        for (String s1 : banned) {
            if (map.containsKey(s1)){
                map.put(s1,0);
            }
        }
        int max = 0;
        String res = "";
        for (String s1 : map.keySet()) {
            if (map.get(s1)>max){
                max =  map.get(s1);
                res = s1;
            }
        }
        return res;
    }
}
